﻿using System;

using Fuse8.DomainFramework.Common.Locator;

using Fuse8.ContentManagementFramework.SystemFacade.Common;

namespace Fuse8.ContentManagementFramework.Web.Etc
{
    public sealed class StorageFacade<T> : IStorage<T>
    {
        private IStorage<T> _storage = null;

        internal IStorage<T> InnerStorage
        {
            get
            {
                if (_storage == null)
                {
                    _storage = DependencyServiceLocator.Current.GetInstance<IStorage<T>>();
                }

                return _storage;
            }
        }

        public StorageFacade()
        {
            if (!DependencyServiceLocator.Current.IsDefaultServiceRegistered<IStorage<T>>())
            {
                DependencyServiceLocator.Current.SetDefaultService<IStorage<T>>(new SessionStorage<T>());
            }
        }

        #region IStorage<T> Members

        public string StorageKey
        {
            get 
            {
                return InnerStorage.StorageKey;
            }
        }

        public T GetProperty()
        {
            return InnerStorage.GetProperty();
        }

        public T GetProperty(string key)
        {
            return InnerStorage.GetProperty(key);
        }

        public void SetProperty(string key, T value)
        {
            InnerStorage.SetProperty(key, value);
        }

        public void SetProperty(T value)
        {
            InnerStorage.SetProperty(value);
        }

        public void Remove()
        {
            InnerStorage.Remove();
        }

        public void Remove(string key)
        {
            InnerStorage.Remove(key);
        }

        #endregion
    }
}
